﻿using System;
using System.Windows.Forms;
using SmartMotion.Core.Mgrs;
using Sunny.UI;

namespace SmartMotion.Core.Tools
{
    public partial class FrmExpression : UIForm
    {
        private readonly ActionExpression _actionExpression;
        private UIComboBox _comboBox;//类型分组
        private UITextBoxTree _tree;
        public FrmExpression(ActionExpression actionExpression)
        {
            _actionExpression = actionExpression;
            InitializeComponent();
        }

        private void FrmExpression_Load(object sender, EventArgs e)
        {
            grid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            _comboBox = new UIComboBox();
            _comboBox.SelectedIndexChanged += _comboBox_SelectedIndexChanged;
            grid.Controls.Add(_comboBox);

            _tree = new UITextBoxTree();
            _tree.ReadOnly = true;
            _tree.ButtonTreeNodeMouseClick += _tree_NodeSelected;
            grid.Controls.Add(_tree);
            txtExp.Text = _actionExpression.BoolExpression;
            if (_actionExpression.ParaNames.Count>0)
            {
                for (int i = 0; i < _actionExpression.ParaNames.Count; i++)
                {
                    grid.Rows.Add(_actionExpression.ParaTypes[i], _actionExpression.ParaNames[i], _actionExpression.Vars[i].Name);
                }
            }
            _comboBox.Visible = false;
            _tree.Visible = false;
        }

        private void _tree_NodeSelected(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (grid.CurrentCell != null)
            {
                string fullPath = e.Node.FullPath.Replace("\\", ".");
                grid.CurrentCell.Value = fullPath;
                //grid.Rows[grid.CurrentCell.RowIndex].Cells[grid.CurrentCell.ColumnIndex].Value = e.Node.Tag.ToString();
                _tree.Visible = false;
            }
        }   

        private void _tree_NodeSelected(object sender, TreeNode node)
        {
            if (grid.CurrentCell != null)
            {
                grid.CurrentCell.Value = node.Text;
                grid.Rows[grid.CurrentCell.RowIndex].Cells[grid.CurrentCell.ColumnIndex + 1].Value = node.Tag.ToString();
            }
        }

        private void _comboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (grid.CurrentCell!=null)
            {
                if (_comboBox.SelectedItem != null) grid.CurrentCell.Value = _comboBox.SelectedItem.ToString();
            }
        }

        private void btnIni_Click(object sender, EventArgs e)
        {
            _actionExpression.ParaTypes.Clear();
            _actionExpression.ParaNames.Clear();
            _actionExpression.Vars.Clear();

            for (int i = 0; i < grid.Rows.Count; i++)
            {
                if (grid.Rows[i].Cells[0].Value!=null && grid.Rows[i].Cells[1].Value != null && grid.Rows[i].Cells[2].Value != null)
                {
                    _actionExpression.ParaTypes.Add(grid.Rows[i].Cells[0].Value.ToString());
                    _actionExpression.ParaNames.Add(grid.Rows[i].Cells[1].Value.ToString());
                    _actionExpression.Vars.Add(new VarInfo(){Name = grid.Rows[i].Cells[2].Value.ToString() });
                }
            }

            _actionExpression.BoolExpression = txtExp.Text;
            _actionExpression.Initilize();
        }

        private void btnRun_Click(object sender, EventArgs e)
        {
            _actionExpression.Run();
        }

        private void grid_CurrentCellChanged(object sender, EventArgs e)
        {
            if (grid.CurrentCell==null) return;
            if (grid.CurrentCell.ColumnIndex==0)
            {
                _comboBox.Items.Clear();
                var typeList = VarMgr.Ins.GetAllTypes();
                if (typeList != null) _comboBox.Items.AddRange(typeList.ToArray());
                int columnIndex = grid.CurrentCell.ColumnIndex;
                int rowIndex = grid.CurrentCell.RowIndex;
                var rect = grid.GetCellDisplayRectangle(columnIndex, rowIndex, false);
                _comboBox.Left = rect.Left;
                _comboBox.Top = rect.Top;
                _comboBox.Width = rect.Width;
                _comboBox.Height = rect.Height;
                var value = grid.CurrentCell.Value;
                if (value==null)
                {
                    _comboBox.SelectedIndex = 0;
                }
                else
                {
                    string consultingRoom = value.ToString();
                    int index = _comboBox.Items.IndexOf(consultingRoom);
                    _comboBox.SelectedIndex = index;
                }

                _comboBox.Visible = true;

            }
            else if (grid.CurrentCell.ColumnIndex == 2)
            {
                if (grid.CurrentRow==null)return;
                var type = grid.CurrentRow.Cells[0].Value;
                if (type==null) return;
                string varType = type.ToString();
                VarMgr.Ins.VarTreeLoadData(_tree,varType);
                int columnIndex = grid.CurrentCell.ColumnIndex;
                int rowIndex = grid.CurrentCell.RowIndex;
                var rect = grid.GetCellDisplayRectangle(columnIndex, rowIndex, false);
                _tree.Left = rect.Left;
                _tree.Top = rect.Top;
                _tree.Width = rect.Width;
                _tree.Height = rect.Height;
                if (null!=grid.Rows[rowIndex].Cells[columnIndex].Value)
                {
                    _tree.Text = grid.Rows[rowIndex].Cells[columnIndex].Value.ToString();
                }
                else
                {
                    _tree.Text = string.Empty;
                }

                _tree.Visible = true;

            }
            else
            {
                _comboBox.Visible = false;
                _tree.Visible = false;
            }
        }

        private void grid_DoubleClick(object sender, EventArgs e)
        {
            grid.Rows.Add();
        }
    }
}
